﻿#include "logwidget.h"
#include "ui_logwidget.h"
#include "logdbo.h"
#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 连接数据库
    // 只要连接了，其他文件怎么写sql语句都行"info_system"
    QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL", "info_system");  // 非静态初始化
    db1.setHostName("127.0.0.1");
    db1.setPort(3306);
    db1.setUserName("root");
    db1.setPassword("Y.j.p203");
    db1.setDatabaseName("info_system");
    if (!db1.open()) {
        QMessageBox::critical(this,tr("提示"),tr("系统连接数据库失败"));
    }
    qDebug() << "数据库连接成功";

    simWin = new StuInfoMntWiget;       // 不要把主窗口绑定到登录窗口，不然会覆盖登录窗口的UI

    // 窗口在屏幕中间显示
    QScreen *screen = QGuiApplication::primaryScreen();  // 获取主屏幕
    QRect screenGeometry = screen->geometry();          // 获取屏幕尺寸

    // 登录窗口切换到主窗口
    connect(this,&Widget::logIn,simWin,[=](){
        this->hide();                   // 隐藏登录窗口
        simWin->show();                 // 显示信息管理窗口
        int x = (screenGeometry.width() - simWin->width()) / 2;
        int y = (screenGeometry.height() - simWin->height()) / 2;
        simWin->move(x, y);  // 移动窗口到屏幕中央
    });

    // 主窗口切换到登录窗口
    connect(simWin,&StuInfoMntWiget::backToLog,this,[=](){
       clearAftLogin();
       clearAftRegister();
       simWin->hide();
       this->show();
       int x = (screenGeometry.width() - this->width()) / 2;
       int y = (screenGeometry.height() - this->height()) / 2;
       this->move(x, y);
    });
}

Widget::~Widget()
{
    delete ui;
    delete simWin;      // 主窗口的内存干掉
}

void Widget::clearAftRegister()
{
    ui->lineEdit_account1->clear();
    ui->lineEdit_username->clear();
    ui->lineEdit_password1->clear();
    ui->lineEdit_password2->clear();
}

void Widget::clearAftLogin()
{
    ui->lineEdit_account->clear();
    ui->lineEdit_password->clear();
}

void Widget::initRegisInput()
{
    ui->lineEdit_account1->setPlaceholderText("请输入账号（11位）");
    ui->lineEdit_username->setPlaceholderText("请输入用户名（3-12个字符）");
    ui->lineEdit_password1->setPlaceholderText("请输入密码（8-12位）");
    ui->lineEdit_password2->setPlaceholderText("请确认密码（8-12位）");
}

void Widget::initLoginInput()
{
    ui->lineEdit_account->setPlaceholderText("请输入账号");
    ui->lineEdit_password->setPlaceholderText("请输入密码");
}

// 注册
void Widget::on_btnRegister_clicked()
{
    qDebug() << "注册";
    // 获取账户和密码
    QString account1 = ui->lineEdit_account1->text();
    QString username = ui->lineEdit_username->text();
    QString password1 = ui->lineEdit_password1->text();
    QString password2 = ui->lineEdit_password2->text();
    // 密码是否一致
    if(password1 != password2){
        ui->lineEdit_password1->clear();
        ui->lineEdit_password2->clear();
        ui->lineEdit_password1->setPlaceholderText("密码不一致，请重新输入");
        return;
    }
    // 输入的账户密码是否为空
    if(account1=="" or username=="" or password1=="" or password2==""){
        QMessageBox::information(this,tr("提示"),tr("注册信息不能为空"));
        return;
    }
    // 输入的账户、密码、用户名是否合法
    if(account1.size()!=11){
        QMessageBox::information(this,tr("提示"),tr("账号长度为11位"));
        return;
    }
    if(username.size()<3 or username.size()>12){
        QMessageBox::information(this,tr("提示"),tr("用户名为3-12个字符"));
        return;
    }
    if(password1.size()<8 or password1.size()>12){
        QMessageBox::information(this,tr("提示"),tr("密码长度为8-12位"));
        return;
    }

    auto ins = LogDBo::getInstance();
    auto user = ins->checkUserInfo(account1,username);

    if(!user){   // sql_select执行失败
        return;
    }
    qDebug() << "user->account: " << user->account;
    qDebug() << "user->username: " << user->username;
    qDebug() << "account1: " << account1;
    qDebug() << "username: " << username;

    // 账户存在
    if(user->account == account1){
        QMessageBox::information(this,tr("提示"),tr("账号已存在，请重新输入"));
        return;
    }
    // 用户名存在
    if(user->username == username){
        QMessageBox::information(this,tr("提示"),tr("用户名已存在，请重新输入"));
        return;
    }
    // 用户和账户不存在，则注册
    user->account = account1;
    user->username = username;
    user->password = password1;
    // 执行sql语句，进行注册
    if(ins->registerUser(user))
        QMessageBox::information(this,tr("提示"),tr("注册成功"));
    else
        QMessageBox::critical(this,tr("错误"),tr("注册失败"));
    clearAftRegister();
    initRegisInput();
}

// 登录
void Widget::on_btnLogin_clicked()
{
   qDebug() << "登录";
   // 获取账号/用户名和密码
   QString account = ui->lineEdit_account->text();
   QString password = ui->lineEdit_password->text();

   // 账户密码是否为空
   if(account=="" or password==""){
       QMessageBox::information(this,tr("提示"),tr("账号或密码不能为空"));
       return;
   }

   // 验证账号和密码
   auto ins = LogDBo::getInstance();
   auto user = ins->checkUserInfo(account,account);
   qDebug() << "user->account: " << user->account;

   if(!user){   // sql_select执行失败
       QMessageBox::critical(this,tr("提示"),tr("系统获取数据失败"));
       return;
   }

   // 账号或密码错误
   if(account != user->account){
       QMessageBox::information(this,tr("提示"),tr("账号错误"));
       return;
   }
   if(password != user->password){
       QMessageBox::information(this,tr("提示"),tr("密码错误"));
       return;
   }
   qDebug() << "登录成功";
   emit this->logIn();
   initRegisInput();
}

// 去注册
void Widget::on_btn2Register_clicked()
{
    clearAftLogin();
    initRegisInput();
    // 转到注册界面
    ui->stackedWidget->setCurrentIndex(1);
}

// 去登录
void Widget::on_btn2Login_clicked()
{
    clearAftRegister();
    initLoginInput();
    // 转到登录界面
    ui->stackedWidget->setCurrentIndex(0);
}
